package com.coffee.wx.uitls.wx;

import cn.hutool.core.map.MapUtil;
import cn.hutool.json.JSONUtil;
import com.coffee.wx.enums.MaterialTypeEnum;
import com.coffee.wx.uitls.SendHttpUtils;
import com.coffee.wx.uitls.TokenUtils;

import java.util.Map;

public class WxMaterialUtils {


    private static final String URL_PREFIX = "https://api.weixin.qq.com/cgi-bin";

    // 上传临时素材
    private static final String UPLOAD_URL_PATH = "/media/upload?access_token=%s&type=%s";

    /**
     * 上传临时素材
     *
     * @param filePath
     * @return
     */
    public static String uploadMaterialByFilePath(String filePath) {

        String response = SendHttpUtils.doPostFileByFilePath(
                String.format(URL_PREFIX + UPLOAD_URL_PATH, TokenUtils.getAccessToken(), MaterialTypeEnum.IMAGE.getType()),
                 filePath, "media");
        Map responseMap = JSONUtil.parse(response).toBean(Map.class);
        return MapUtil.getStr(responseMap, "media_id", null);
    }
}
